package com.basic.business.utils;

import com.basic.business.common.constant.ResultMsgEnum;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

@ApiModel(value = "返回类")
@Data
public class Result<T> {
    @ApiModelProperty(value = "返回code")
    protected int retCode;
    @ApiModelProperty(value = "返回message")
    protected String retMessage;
    @ApiModelProperty(value = "返回数据")
    protected T retData;
    @ApiModelProperty(value = "返回数量")
    protected int totalNum;

    public Result(int retCode, T retData, int totalNum) {
        this.retCode = retCode;
        this.retData = retData;
        this.totalNum = totalNum;
        return;
    }

    public Result(int retCode, T retData) {
        this(retCode, retData, 1);
    }

    public Result(int retCode, String retMessage) {
        this.retCode = retCode;
        this.retMessage = retMessage;
        return;
    }

    public Result() {

    }

    public Result(int retCode, String retMessage, T retData) {
        this.retCode = retCode;
        this.retMessage = retMessage;
        this.retData = retData;
    }

    /**
     * 成功
     *
     * @param data
     * @return
     */
    public static <T> Result<T> success(T data) {
        Result<T> result = new Result<T>();
        result.setRetCode(ResultMsgEnum.SUCCESS.getCode());
        result.setRetMessage(ResultMsgEnum.SUCCESS.getMessage());
        result.setRetData(data);
        return result;
    }

    public static Result<String> success() {
        Result result = new Result();
        result.setRetCode(ResultMsgEnum.SUCCESS.getCode());
        result.setRetMessage(ResultMsgEnum.SUCCESS.getMessage());
        return result;
    }

    /**
     * 失败
     *
     * @param returnMessage
     * @return
     */
    public static Result<String> error(ResultMsgEnum returnMessage) {
        return new Result(returnMessage.getCode(), returnMessage.getMessage());
    }

    public static <T> Result<T> error(ResultMsgEnum returnMessage, T data) {
        return new Result(returnMessage.getCode(), returnMessage.getMessage(), data);
    }

    /**
     * 失败
     *
     * @param retCode
     * @param retMessage
     * @return
     */
    public static Result error(int retCode, String retMessage) {
        return new Result(retCode, retMessage);
    }


    public static <T> Result<T> error(int retCode, String retMessage, T retData) {
        return new Result(retCode, retMessage, retData);
    }
}